package demo1.Frames;

import demo1.Dialogs.SinDialog;
import demo1.Panels.MySinLine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class SinLineTest extends JFrame {
    JPanel root = new JPanel();
    public MySinLine sinLine = new MySinLine();
    public SinLineTest(String title) {
        super(title);
        this.setContentPane(root);
        InitSinLine();
    }
    private void InitSinLine() {
        root.add(sinLine);
        sinLine.setPreferredSize(new Dimension(300, 150));
        sinLine.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                if(e.getButton() == MouseEvent.BUTTON3) {
                    showMenu(e);
                }
            }
        });
    }
    private void showMenu(MouseEvent e) {
        JPopupMenu menu = new JPopupMenu();

        JLabel label = new JLabel("参数设置");
        menu.add(label);

        JMenuItem item1 = new JMenuItem("颜色");
        item1.addActionListener(new ItemListener());
        item1.setActionCommand("color");
        menu.add(item1);

        JMenuItem item2 = new JMenuItem("振幅A");
        item2.addActionListener(new ItemListener());
        item2.setActionCommand("A");
        menu.add(item2);

        JMenuItem item3 = new JMenuItem("周期T");
        item3.addActionListener(new ItemListener());
        item3.setActionCommand("T");
        menu.add(item3);

        menu.show(e.getComponent(),e.getX(),e.getY());
    }

    private class ItemListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            SinDialog sinDialog = new SinDialog();
            boolean ret = sinDialog.show(sinDialog);
            if(ret) {
                if(e.getActionCommand().equals("color")) {
                    repaint();
                }
                if(e.getActionCommand().equals("A")) {
                    repaint();
                }
                if(e.getActionCommand().equals("T")) {
                    System.out.println("T");
                    repaint();
                }
            }
        }
    }
}
